package com.minisoft.pool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomerAllocation {

    private static final int DAILY_LIMIT = 10; // 每个顾问每天的分配限额

    public static void main(String[] args) {
        // 加载顾问列表和其已分配数量
        List<Consultant> consultants = loadConsultants();

        // 加载客户列表
        List<String> customers = new ArrayList<>();
        customers.add("Customer1");
        customers.add("Customer2");
        customers.add("Customer3");
        customers.add("Customer4");
        customers.add("Customer5");

        Map<String, Integer> allocations = allocateCustomers(customers, consultants);

        int totalCustomers = customers.size();
        int successfulAllocations = allocations.size();

        System.out.println("Total Customers: " + totalCustomers);
        System.out.println("Successful Allocations: " + successfulAllocations);
        System.out.println("Over limited consultants: " + getOverLimitedConsultants(consultants, allocations));
    }

    private static List<Consultant> loadConsultants() {
        // 从数据库或其他数据源加载顾问信息，包括顾问名字和已分配客户数量
        List<Consultant> consultants = new ArrayList<>();

        // 假设从数据库中加载了以下顾问及其已分配数量
        consultants.add(new Consultant("Consultant1", 5));
        consultants.add(new Consultant("Consultant2", 3));
        consultants.add(new Consultant("Consultant3", 2));

        return consultants;
    }

    private static Map<String, Integer> allocateCustomers(List<String> customers, List<Consultant> consultants) {
        Map<String, Integer> allocations = new HashMap<>();
        int totalCustomerCount = customers.size();

        // 根据顾问已分配数量，计算剩余分配限额
        List<Consultant> consultantsWithRemainingQuota = new ArrayList<>();
        for (Consultant consultant : consultants) {
            int remainingQuota = DAILY_LIMIT - consultant.getAllocatedCount();
            if (remainingQuota > 0) {
                consultantsWithRemainingQuota.add(consultant);
            }
        }

        int totalConsultantCount = consultantsWithRemainingQuota.size();
        int customerIndex = 0;
        int consultantIndex = 0;
        int allocatedCount = 0;

        while (allocatedCount < totalCustomerCount && totalConsultantCount > 0) {
            String customer = customers.get(customerIndex);
            Consultant consultant = consultantsWithRemainingQuota.get(consultantIndex);

            int currentAllocation = allocations.getOrDefault(consultant.getName(), 0);
            allocations.put(consultant.getName(), currentAllocation + 1);

            consultant.incrementAllocatedCount();
            customerIndex++;
            consultantIndex = (consultantIndex + 1) % totalConsultantCount;
            allocatedCount++;

            if (consultant.getAllocatedCount() == DAILY_LIMIT) {
                totalConsultantCount--;
            }
        }

        return allocations;
    }

    private static List<String> getOverLimitedConsultants(List<Consultant> consultants, Map<String, Integer> allocations) {
        List<String> overLimitedConsultants = new ArrayList<>();

        for (Consultant consultant : consultants) {
            int allocatedCount = allocations.getOrDefault(consultant.getName(), 0);
            if (allocatedCount >= DAILY_LIMIT) {
                overLimitedConsultants.add(consultant.getName());
            }
        }

        return overLimitedConsultants;
    }

    static class Consultant {
        private String name;
        private int allocatedCount;

        public Consultant(String name, int allocatedCount) {
            this.name = name;
            this.allocatedCount = allocatedCount;
        }

        public String getName() {
            return name;
        }

        public int getAllocatedCount() {
            return allocatedCount;
        }

        public void incrementAllocatedCount() {
            allocatedCount++;
        }
    }
}